package org.gdstash.ui;

import org.gdstash.db.DBStashItem;
import org.gdstash.db.DBStat;
import org.gdstash.db.SelectionCriteria;
import org.gdstash.db.criteria.CriteriaCombination;
import org.gdstash.item.GDItem;
import org.gdstash.ui.stash.GDContainerMapPane;
import org.gdstash.ui.stash.GDSharedStashPane;
import org.gdstash.ui.util.AdjustablePanel;
import org.gdstash.ui.util.GDCharInfoList;
import org.gdstash.ui.util.GDStashInfoList;
import org.gdstash.util.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

public class GDTransferPane extends AdjustablePanel implements GDUITransfer, GDUISearch {
    private static final int STASHTYPE_UNKNOWN = 0;
    private static final int STASHTYPE_SOFTCORE = 1;
    private static final int STASHTYPE_HARDCORE = 2;
    private GDStashFrame frame;
    private GDSharedStashPane pnlStash;
    private GDItemInfoPane pnlInfo;
    private JButton btnDBCopy;
    private JButton btnDBMove;
    private JButton btnDBDel;
    private JButton btnStashDel;
    private JButton btnPageCopy;
    private JButton btnPageMove;
    private JButton btnPageDel;
    private JButton btnReload;
    private JButton btnHelp;
    private JButton btnFileSave;
    private GDItemNameTablePane pnlTable;
    private String strSave;
    private JButton btnSearch;
    private GDStashInfoList.GDStashFileInfo info;
    private GDItem selItem;
    private int location;
    private JPanel pnlMain;
    private GDTabbedSearchDialog dlgSearch;
    private int stashType;

    public GDTransferPane(final GDStashInfoList.GDStashFileInfo info, final GDStashFrame frame) {
        this.info = info;
        this.frame = frame;
        this.determineStashType();
        this.strSave = GDStashFrame.iniConfig.sectDir.savePath;
        this.selItem = null;
        this.location = 0;
        this.dlgSearch = null;
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        this.pnlMain = this.buildMainPanel();
        final JScrollPane scroll = new JScrollPane(this.pnlMain);
        scroll.getVerticalScrollBar().setUnitIncrement(2 * GDStashFrame.iniConfig.sectUI.fontSize);
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(scroll));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(scroll));
        layout.setVerticalGroup(vGroup);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntButton = UIManager.getDefaults().getFont("Button.font");
        if (fntButton == null) {
            fntButton = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntButton = fntButton.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        if (this.btnFileSave == null) {
            (this.btnFileSave = new JButton()).setEnabled(false);
            this.btnFileSave.addActionListener(new FileSaveActionListener());
        }
        this.btnFileSave.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BTN_STASH_SAVE"));
        this.btnFileSave.setIcon(GDImagePool.iconBtnSave24);
        this.btnFileSave.setFont(fntButton);
        GDStashFrame.setMnemonic(this.btnFileSave, "MNC_STASH_SAVE");
        if (this.btnDBDel == null) {
            (this.btnDBDel = new JButton()).setEnabled(false);
            this.btnDBDel.addActionListener(new DBDelActionListener());
        }
        this.btnDBDel.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BTN_ITEM_DEL_DB"));
        this.btnDBDel.setIcon(GDImagePool.iconDBDelete24);
        this.btnDBDel.setFont(fntButton);
        GDStashFrame.setMnemonic(this.btnDBDel, "MNC_ITEM_DEL_DB");
        if (this.btnDBCopy == null) {
            (this.btnDBCopy = new JButton()).setEnabled(false);
            this.btnDBCopy.addActionListener(new DBCopyActionListener());
        }
        this.btnDBCopy.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BTN_ITEM_COPY_DB"));
        this.btnDBCopy.setIcon(GDImagePool.iconItemCopy24);
        this.btnDBCopy.setFont(fntButton);
        GDStashFrame.setMnemonic(this.btnDBCopy, "MNC_ITEM_COPY_DB");
        if (this.btnDBMove == null) {
            (this.btnDBMove = new JButton()).setEnabled(false);
            this.btnDBMove.addActionListener(new DBMoveActionListener());
        }
        this.btnDBMove.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BTN_ITEM_MOVE_DB"));
        this.btnDBMove.setIcon(GDImagePool.iconItemMove24);
        this.btnDBMove.setFont(fntButton);
        GDStashFrame.setMnemonic(this.btnDBMove, "MNC_ITEM_MOVE_DB");
        if (this.btnPageCopy == null) {
            (this.btnPageCopy = new JButton()).setEnabled(true);
            this.btnPageCopy.addActionListener(new PageCopyActionListener());
        }
        this.btnPageCopy.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BTN_PAGE_COPY_DB"));
        this.btnPageCopy.setIcon(GDImagePool.iconPageCopy24);
        this.btnPageCopy.setFont(fntButton);
        if (this.btnPageMove == null) {
            (this.btnPageMove = new JButton()).setEnabled(true);
            this.btnPageMove.addActionListener(new PageMoveActionListener());
        }
        this.btnPageMove.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BTN_PAGE_MOVE_DB"));
        this.btnPageMove.setIcon(GDImagePool.iconPageMove24);
        this.btnPageMove.setFont(fntButton);
        if (this.btnStashDel == null) {
            (this.btnStashDel = new JButton()).setEnabled(false);
            this.btnStashDel.addActionListener(new StashDelActionListener());
        }
        this.btnStashDel.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BTN_ITEM_DEL_STASH"));
        this.btnStashDel.setIcon(GDImagePool.iconItemDelete24);
        this.btnStashDel.setFont(fntButton);
        GDStashFrame.setMnemonic(this.btnStashDel, "MNC_ITEM_DEL_STASH");
        if (this.btnPageDel == null) {
            (this.btnPageDel = new JButton()).setEnabled(true);
            this.btnPageDel.addActionListener(new PageDelActionListener());
        }
        this.btnPageDel.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BTN_PAGE_DEL"));
        this.btnPageDel.setIcon(GDImagePool.iconPageDelete24);
        this.btnPageDel.setFont(fntButton);
        if (this.btnSearch == null) {
            (this.btnSearch = new JButton()).addActionListener(new SearchItemsListener());
        }
        this.btnSearch.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BTN_SEARCH"));
        this.btnSearch.setIcon(GDImagePool.iconBtnSearch24);
        this.btnSearch.setFont(fntButton);
        GDStashFrame.setMnemonic(this.btnSearch, "MNC_SEARCH_SECOND");
        if (this.btnReload == null) {
            (this.btnReload = new JButton()).addActionListener(new StashReloadActionListener());
        }
        this.btnReload.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BTN_RELOAD"));
        this.btnReload.setIcon(GDImagePool.iconBtnReload24);
        this.btnReload.setFont(fntButton);
        GDStashFrame.setMnemonic(this.btnReload, "MNC_RELOAD");
        if (this.btnHelp == null) {
            (this.btnHelp = new JButton()).addActionListener(new GDHelpActionListener("03_transfer.html"));
        }
        this.btnHelp.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BTN_HELP"));
        this.btnHelp.setIcon(GDImagePool.iconBtnQuestion24);
        this.btnHelp.setFont(fntButton);
        GDStashFrame.setMnemonic(this.btnHelp, "MNC_HELP");
        if (this.pnlInfo == null) {
            this.pnlInfo = new GDItemInfoPane();
        }
        this.pnlInfo.adjustUI();
        if (this.pnlTable == null) {
            (this.pnlTable = new GDItemNameTablePane((List<GDItem>) null, false)).addActionListener(new TableActionListener());
            this.pnlTable.setMinimumSize(new Dimension(300, 200));
            this.pnlTable.setMaximumSize(new Dimension(600, 2000));
        } else {
            this.pnlTable.adjustUI();
        }
        if (this.pnlStash == null) {
            this.pnlStash = new GDSharedStashPane(this.info, 1, this.frame, this);
        } else {
            this.pnlStash.adjustUI();
        }
        if (this.dlgSearch != null) {
            this.dlgSearch.adjustUI();
        }
    }

    @Override
    public void updateUI() {
        super.updateUI();
        if (this.dlgSearch != null) {
            SwingUtilities.updateComponentTreeUI(this.dlgSearch);
        }
    }

    public void refresh() {
        if (this.pnlTable != null) {
            this.pnlTable.refresh();
        }
        if (this.pnlInfo != null) {
            this.pnlInfo.refresh();
        }
        if (this.pnlStash != null) {
            this.pnlStash.refresh();
        }
    }

    private JPanel buildMainPanel() {
        final JPanel panel = new JPanel();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        final JPanel pnlTable = this.buildTablePanel();
        final JPanel pnlItemInfo = this.buildInfoPanel();
        layout = new GroupLayout(panel);
        panel.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(pnlTable)).addGroup(layout.createParallelGroup().addComponent(pnlItemInfo)).addGroup(layout.createParallelGroup().addComponent(this.pnlStash));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(pnlTable).addComponent(pnlItemInfo).addComponent(this.pnlStash));
        layout.setVerticalGroup(vGroup);
        return panel;
    }

    private JPanel buildTablePanel() {
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        final JPanel panel = new JPanel();
        layout = new GroupLayout(panel);
        panel.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.btnSearch).addComponent(this.pnlTable));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.btnSearch)).addGroup(layout.createParallelGroup().addComponent(this.pnlTable));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(0, this.btnSearch, this.pnlTable);
        return panel;
    }

    private JPanel buildInfoPanel() {
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        final JPanel panel = new JPanel();
        final JPanel pnlButtonFile = this.buildFileButtonPanel();
        final JPanel pnlButtonOper = this.buildOperationButtonPanel();
        layout = new GroupLayout(panel);
        panel.setLayout(layout);
        layout.setAutoCreateGaps(false);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(pnlButtonFile).addComponent(pnlButtonOper).addComponent(this.pnlInfo));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(pnlButtonFile)).addGroup(layout.createParallelGroup().addComponent(pnlButtonOper)).addGroup(layout.createParallelGroup().addComponent(this.pnlInfo));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(0, pnlButtonFile, pnlButtonOper);
        layout.linkSize(0, pnlButtonFile, this.pnlInfo);
        return panel;
    }

    private JPanel buildFileButtonPanel() {
        final BorderLayout layout = new BorderLayout();
        final JPanel panel = new JPanel();
        panel.setLayout(layout);
        panel.add(this.btnFileSave, "Center");
        panel.add(this.btnReload, "West");
        panel.add(this.btnHelp, "East");
        int size = 12;
        if (GDStashFrame.iniConfig != null) {
            size = GDStashFrame.iniConfig.sectUI.fontSize;
        }
        final Dimension dimPref = new Dimension(30 * size, 2 * size);
        final Dimension dimMax = new Dimension(50 * size, 2 * size);
        panel.setPreferredSize(dimPref);
        panel.setMaximumSize(dimMax);
        return panel;
    }

    private JPanel buildOperationButtonPanel() {
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        final JPanel panel = new JPanel();
        layout = new GroupLayout(panel);
        panel.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.btnDBCopy).addComponent(this.btnDBMove).addComponent(this.btnStashDel).addComponent(this.btnDBDel)).addGroup(layout.createParallelGroup().addComponent(this.btnPageCopy).addComponent(this.btnPageMove).addComponent(this.btnPageDel));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.btnDBCopy).addComponent(this.btnPageCopy)).addGroup(layout.createParallelGroup().addComponent(this.btnDBMove).addComponent(this.btnPageMove)).addGroup(layout.createParallelGroup().addComponent(this.btnStashDel).addComponent(this.btnPageDel)).addGroup(layout.createParallelGroup().addComponent(this.btnDBDel));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(0, this.btnDBCopy, this.btnDBMove);
        layout.linkSize(0, this.btnDBCopy, this.btnStashDel);
        layout.linkSize(0, this.btnDBCopy, this.btnDBDel);
        layout.linkSize(0, this.btnDBCopy, this.btnPageCopy);
        layout.linkSize(0, this.btnDBCopy, this.btnPageMove);
        layout.linkSize(0, this.btnDBCopy, this.btnPageDel);
        layout.linkSize(1, this.btnDBCopy, this.btnDBMove);
        layout.linkSize(1, this.btnDBCopy, this.btnStashDel);
        layout.linkSize(1, this.btnDBCopy, this.btnDBDel);
        layout.linkSize(1, this.btnDBCopy, this.btnPageCopy);
        layout.linkSize(1, this.btnDBCopy, this.btnPageMove);
        layout.linkSize(1, this.btnDBCopy, this.btnPageDel);
        return panel;
    }

    private JPanel buildButtonGridPanel() {
        GridLayout layout = null;
        final JPanel panel = new JPanel();
        layout = new GridLayout(4, 2, 10, 10);
        panel.setLayout(layout);
        panel.add(this.btnDBCopy);
        panel.add(this.btnPageCopy);
        panel.add(this.btnDBMove);
        panel.add(this.btnPageMove);
        panel.add(this.btnStashDel);
        panel.add(this.btnPageDel);
        panel.add(this.btnDBDel);
        return panel;
    }

    private void determineStashType() {
        this.stashType = 0;
        if (this.info != null && this.info.gdStash != null) {
            if (this.info.gdStash.isHardcore()) {
                this.stashType = 2;
            } else {
                this.stashType = 1;
            }
        }
    }

    public void clearSelItems() {
        this.pnlInfo.setItemInfo(null, false, "");
        this.pnlTable.setItems(null);
    }

    public void updateStash() {
        this.pnlStash.layoutStash();
        this.pnlStash.updateModSelection();
    }

    public void updateConfig() {
        if (this.dlgSearch != null) {
            this.dlgSearch.updateConfig();
        }
    }

    public void refreshStashSelection() {
        this.pnlStash.refreshStashSelection();
    }

    public void setStashInfo(final GDStashInfoList.GDStashFileInfo info) {
        this.info = info;
        if (this.pnlStash != null) {
            this.pnlStash.setStash(info);
        }
        this.doLayout();
    }

    public void refreshStashInfo(final GDStashInfoList.GDStashFileInfo info) {
        if (info == null) {
            return;
        }
        if (this.info == null) {
            return;
        }
        if (this.info.fileName.equals(info.fileName)) {
            this.info = info;
            if (this.pnlStash != null) {
                this.pnlStash.setStash(info);
            }
        }
    }

    public boolean updateStashFile() {
        if (this.info == null) {
            return false;
        }
        if (this.info.gdStash == null) {
            return false;
        }
        if (GDStashFrame.iniConfig == null) {
            return false;
        }
        if (GDStashFrame.iniConfig.sectDir.savePath == null) {
            return false;
        }
        try {
            String stashMod = this.info.gdStash.getModName();
            if (stashMod == null) {
                stashMod = "";
            }
            String dir = this.info.gdStash.getFile().getParentFile().getCanonicalPath();
            if (dir.length() > GDStashFrame.iniConfig.sectDir.savePath.length()) {
                dir = dir.substring(GDStashFrame.iniConfig.sectDir.savePath.length() + 1);
            } else {
                dir = "";
            }
            if (stashMod.equals(dir)) {
                return false;
            }
            dir = GDStashFrame.iniConfig.sectDir.savePath;
            if (!dir.endsWith(GDConstants.FILE_SEPARATOR)) {
                dir += GDConstants.FILE_SEPARATOR;
            }
            if (!stashMod.equals("")) {
                dir = dir + stashMod + GDConstants.FILE_SEPARATOR;
            }
            dir += this.info.stashFile.getName();
            final File file = new File(dir);
            this.info.gdStash.setFile(file);
            return true;
        } catch (IOException ex) {
            return false;
        }
    }

    @Override
    public void setChar(final GDCharInfoList.GDCharFileInfo info) {
    }

    @Override
    public void setStash(final GDStashInfoList.GDStashFileInfo info) {
        if (this.info == info) {
            return;
        }
        this.info = info;
        final int oldStashType = this.stashType;
        this.determineStashType();
        if (!GDStashFrame.iniConfig.sectRestrict.transferSCHC && oldStashType != 0 && oldStashType != this.stashType) {
            this.pnlTable.setItems(null);
        }
        this.checkSaveButton();
    }

    @Override
    public int getItemLocation() {
        return this.location;
    }

    @Override
    public GDItem getSelectedItem() {
        return this.selItem;
    }

    @Override
    public void setSelectedItem(final GDItem item, final int location) {
        if (location == 1) {
            this.pnlTable.clearSelection();
        }
        this.selItem = item;
        this.location = location;
        boolean hardcore = false;
        if (this.info != null && this.info.gdStash != null) {
            hardcore = this.info.gdStash.isHardcore();
        }
        this.pnlInfo.setItemInfo(item, hardcore, "");
        switch (location) {
            case 1: {
                this.btnDBCopy.setEnabled(item != null);
                this.btnDBMove.setEnabled(item != null);
                this.btnStashDel.setEnabled(item != null);
                this.btnDBDel.setEnabled(false);
                this.btnPageCopy.setEnabled(true);
                this.btnPageMove.setEnabled(true);
                this.btnPageDel.setEnabled(true);
                break;
            }
            case 2: {
                this.btnDBCopy.setEnabled(false);
                this.btnDBMove.setEnabled(false);
                this.btnStashDel.setEnabled(false);
                this.btnDBDel.setEnabled(item != null);
                this.btnPageCopy.setEnabled(false);
                this.btnPageMove.setEnabled(false);
                this.btnPageDel.setEnabled(false);
                break;
            }
            default: {
                this.btnDBCopy.setEnabled(false);
                this.btnDBMove.setEnabled(false);
                this.btnStashDel.setEnabled(false);
                this.btnDBDel.setEnabled(false);
                this.btnPageCopy.setEnabled(true);
                this.btnPageMove.setEnabled(true);
                this.btnPageDel.setEnabled(true);
                break;
            }
        }
    }

    @Override
    public void transferSelectedItem(final int action, final int x, final int y) {
        switch (action) {
            case 1: {
                if (this.location == 1) {
                    final GDItem item = this.pnlStash.getSelectedItem();
                    if (item != null) {
                        this.pnlStash.moveSelectedItem(1, x, y);
                        this.btnFileSave.setEnabled(this.info.gdStash.hasChanged());
                    }
                    break;
                }
                break;
            }
            case 2: {
                if (this.location == 1) {
                    final boolean success = DBStashItem.storeItem(this.selItem);
                    if (success) {
                        this.setSelectedItem(null, 0);
                    }
                    GDMsgLogger.showLog(this.frame, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "SUCC_ITEM_STORED"), GDLog.MessageType.Success, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERR_ITEM_STORED"));
                    break;
                }
                break;
            }
            case 3: {
                if (this.location == 1) {
                    final boolean success = DBStashItem.storeItem(this.selItem);
                    if (success) {
                        this.pnlStash.deleteSelectedItem(3);
                        this.setSelectedItem(null, 0);
                        this.btnFileSave.setEnabled(this.info.gdStash.hasChanged());
                    }
                    GDMsgLogger.showLog(this.frame, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "SUCC_ITEM_STORED"), GDLog.MessageType.Success, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERR_ITEM_STORED"));
                    break;
                }
                break;
            }
            case 4: {
                if (this.location == 2) {
                    final boolean success = this.pnlStash.addItem(this.selItem, 4, x, y);
                    if (success && GDStashFrame.iniConfig.sectRestrict.dbStashMove) {
                        this.transferSelectedItem(5, x, y);
                    }
                    this.btnFileSave.setEnabled(this.info.gdStash.hasChanged());
                    break;
                }
                break;
            }
            case 5: {
                if (this.location == 2) {
                    final boolean success = DBStashItem.delete(this.selItem);
                    if (success) {
                        if (this.selItem.isStackable()) {
                            final GDItem item2 = DBStashItem.getStack(this.selItem);
                            this.pnlTable.updateItem(item2);
                        } else {
                            this.pnlTable.deleteItem();
                        }
                        this.setSelectedItem(this.pnlTable.getSelectedItem(), 2);
                    }
                    GDMsgLogger.showLog(this.frame, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "SUCC_ITEM_DELETED"), GDLog.MessageType.Success, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERR_ITEM_DELETED"));
                    break;
                }
                break;
            }
            case 6: {
                if (this.location != 1) {
                    break;
                }
                final GDItem item = this.getSelectedItem();
                if (item == null) {
                    break;
                }
                this.pnlStash.deleteSelectedItem(6);
                this.setSelectedItem(null, 0);
                this.btnFileSave.setEnabled(this.info.gdStash.hasChanged());
                break;
            }
            case 7: {
                if (this.location != 1 && this.location != 0) {
                    break;
                }
                final GDContainerMapPane page = this.pnlStash.getCurrentPage();
                if (page == null) {
                    return;
                }
                final List<GDItem> items = page.getItemList(7);
                for (final GDItem item3 : items) {
                    DBStashItem.storeItem(item3);
                }
                GDMsgLogger.showLog(this.frame, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "SUCC_ITEM_STORED"), GDLog.MessageType.Success, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERR_ITEM_STORED"));
                break;
            }
            case 8: {
                if (this.location != 1 && this.location != 0) {
                    break;
                }
                final GDContainerMapPane page = this.pnlStash.getCurrentPage();
                if (page == null) {
                    return;
                }
                final List<GDItem> items = page.getItemList(8);
                for (final GDItem item3 : items) {
                    final boolean success2 = DBStashItem.storeItem(item3);
                    if (success2) {
                        page.deleteItem(item3, 8, false);
                    }
                }
                page.layoutContainers();
                this.setSelectedItem(null, 0);
                this.btnFileSave.setEnabled(this.info.gdStash.hasChanged());
                GDMsgLogger.showLog(this.frame, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "SUCC_ITEM_STORED"), GDLog.MessageType.Success, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERR_ITEM_STORED"));
                break;
            }
            case 9: {
                if (this.location != 1 && this.location != 0) {
                    break;
                }
                final GDContainerMapPane page = this.pnlStash.getCurrentPage();
                if (page == null) {
                    return;
                }
                final List<GDItem> items = page.getItemList(9);
                for (final GDItem item3 : items) {
                    page.deleteItem(item3, 9, false);
                }
                page.layoutContainers();
                this.setSelectedItem(null, 0);
                this.btnFileSave.setEnabled(this.info.gdStash.hasChanged());
                GDMsgLogger.showLog(this.frame, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "SUCC_ITEM_DELETED"), GDLog.MessageType.Success, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERR_ITEM_DELETED"));
                break;
            }
        }
    }

    @Override
    public void checkSaveButton() {
        if (this.info == null) {
            this.btnFileSave.setEnabled(false);
        } else if (this.info.gdStash == null) {
            this.btnFileSave.setEnabled(false);
        } else {
            this.btnFileSave.setEnabled(this.info.gdStash.hasChanged());
        }
    }

    @Override
    public Frame getFrame() {
        return this.frame;
    }

    @Override
    public void search(final SelectionCriteria criteria) {
        if (this.info == null) {
            return;
        }
        if (this.info.gdStash == null) {
            return;
        }
        final boolean hardcore = this.info.gdStash.isHardcore();
        criteria.noEnemyOnly = true;
        List<GDItem> items = DBStashItem.getGDItemByCriteria(criteria, CriteriaCombination.Soulbound.INCLUDED, CriteriaCombination.SC_HC.SETTING, hardcore, null);
        if (criteria.combiMode == SelectionCriteria.CombinationMode.AND) {
            final List<GDItem> list = new LinkedList<GDItem>();
            for (final GDItem gdi : items) {
                if (!DBStat.statsMeetCriteria(gdi.getStatList(), criteria)) {
                    continue;
                }
                if (criteria.dmgConversionFrom != null && !gdi.hasConvertIn(criteria.dmgConversionFrom)) {
                    continue;
                }
                if (criteria.dmgConversionTo != null && !gdi.hasConvertOut(criteria.dmgConversionTo)) {
                    continue;
                }
                if (criteria.petBonus && !gdi.hasPetBonus()) {
                    continue;
                }
                list.add(gdi);
            }
            items = list;
        }
        this.pnlTable.setItems(items);
        GDMsgLogger.showLog(this, GDMsgFormatter.getString(GDMsgFormatter.rbMsg, "ERR_SEARCH"));
    }

    private class TableActionListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent ev) {
            final GDItem item = GDTransferPane.this.pnlTable.getSelectedItem();
            GDTransferPane.this.setSelectedItem(item, 2);
            GDMsgLogger.showLog(GDTransferPane.this, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERRORS"));
        }
    }

    private class StashReloadActionListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent ev) {
            if (GDTransferPane.this.info.gdStash != null && GDTransferPane.this.info.gdStash.hasChanged() && GDStashFrame.iniConfig.sectRestrict.dbStashMove) {
                final JButton[] buttons = {new JButton(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BTN_YES"), GDImagePool.iconBtnOk24), new JButton(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BTN_NO"), GDImagePool.iconBtnCancel24)};
                final String tag = "TXT_RELOAD_STASH";
                final int i = GDFlexDialog.showDialog(GDMsgFormatter.getString(GDMsgFormatter.rbUI, tag), buttons, 3, null, true);
                if (i == -1) {
                    return;
                }
                if (i == 1) {
                    return;
                }
            }
            GDStashInfoList.findStashes(GDTransferPane.this.frame, GDTransferPane.this.info, null);
            if (GDTransferPane.this.info != null) {
                for (final GDStashInfoList.GDStashFileInfo si : GDStashInfoList.gdStashFileInfos) {
                    if (si.fileName.equals(GDTransferPane.this.info.fileName)) {
                        GDTransferPane.this.info = si;
                        break;
                    }
                }
            }
            if (GDTransferPane.this.frame.pnlTransfer != null) {
                GDTransferPane.this.frame.pnlTransfer.refreshStashInfo(GDTransferPane.this.info);
            }
            if (GDTransferPane.this.frame.pnlCraft != null) {
                GDTransferPane.this.frame.pnlCraft.refreshStashInfo(GDTransferPane.this.info);
            }
            GDMsgLogger.showLog(GDTransferPane.this, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "MESSAGES"), GDLog.MessageType.Info, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERRORS"), false, false);
        }
    }

    private class DBDelActionListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent ev) {
            final GDItem item = GDTransferPane.this.pnlTable.getSelectedItem();
            if (item == null) {
                return;
            }
            GDTransferPane.this.transferSelectedItem(5, 0, 0);
            GDMsgLogger.showLog(GDTransferPane.this, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERRORS"));
        }
    }

    private class DBCopyActionListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent ev) {
            final GDItem item = GDTransferPane.this.getSelectedItem();
            if (item == null) {
                return;
            }
            GDTransferPane.this.transferSelectedItem(2, 0, 0);
            GDMsgLogger.showLog(GDTransferPane.this, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERRORS"));
        }
    }

    private class DBMoveActionListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent ev) {
            final GDItem item = GDTransferPane.this.getSelectedItem();
            if (item == null) {
                return;
            }
            GDTransferPane.this.transferSelectedItem(3, 0, 0);
            GDMsgLogger.showLog(GDTransferPane.this, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERRORS"));
        }
    }

    private class PageCopyActionListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent ev) {
            if (GDTransferPane.this.pnlStash.getCurrentPage() == null) {
                return;
            }
            GDTransferPane.this.transferSelectedItem(7, 0, 0);
            GDMsgLogger.showLog(GDTransferPane.this, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERRORS"));
        }
    }

    private class PageMoveActionListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent ev) {
            if (GDTransferPane.this.pnlStash.getCurrentPage() == null) {
                return;
            }
            GDTransferPane.this.transferSelectedItem(8, 0, 0);
            GDMsgLogger.showLog(GDTransferPane.this, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERRORS"));
        }
    }

    private class StashDelActionListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent ev) {
            GDTransferPane.this.transferSelectedItem(6, 0, 0);
            GDMsgLogger.showLog(GDTransferPane.this, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERRORS"));
        }
    }

    private class PageDelActionListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent ev) {
            if (GDTransferPane.this.pnlStash.getCurrentPage() == null) {
                return;
            }
            GDTransferPane.this.transferSelectedItem(9, 0, 0);
            GDMsgLogger.showLog(GDTransferPane.this, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERRORS"));
        }
    }

    private class FileSaveActionListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent ev) {
            if (GDTransferPane.this.info == null) {
                return;
            }
            if (GDTransferPane.this.info.gdStash == null) {
                return;
            }
            if (GDTransferPane.this.frame.isCloudSaveDir() && GDStashFrame.isGrimDawnRunning()) {
                GDMsgLogger.addInfo(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "INFO_GD_CLOUD_SAVE"));
                GDMsgLogger.addInfo(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "INFO_GD_CLOSE"));
                GDMsgLogger.showLog(GDTransferPane.this.frame, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "INFO_GD_RUNNING"), GDLog.MessageType.Info, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "INFO_GD_RUNNING"));
                return;
            }
            boolean success = false;
            try {
                final boolean update = GDTransferPane.this.updateStashFile();
                GDTransferPane.this.info.gdStash.write();
                if (update) {
                    GDTransferPane.this.info.setFile(GDTransferPane.this.info.gdStash.getFile());
                    GDStashFrame.iniConfig.sectHistory.lastStash = GDTransferPane.this.info.fileName;
                    GDTransferPane.this.btnReload.doClick();
                }
                success = true;
            } catch (IOException ex) {
                GDMsgLogger.addError(ex);
            }
            if (success) {
                GDTransferPane.this.btnFileSave.setEnabled(GDTransferPane.this.info.gdStash.hasChanged());
            }
            GDMsgLogger.showLog(GDTransferPane.this.frame, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "SUCC_STASH_SAVE"), GDLog.MessageType.Success, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERR_STASH_SAVE"));
        }
    }

    private class SearchItemsListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            if (GDTransferPane.this.dlgSearch == null) {
                GDTransferPane.this.dlgSearch = new GDTabbedSearchDialog(GDTransferPane.this.frame, GDTransferPane.this, GDTabbedSearchDialog.Mode.TRANSFER, SelectionCriteria.SoulboundMode.NONBOUND);
            }
            GDTransferPane.this.dlgSearch.setVisible(true);
        }
    }
}
